#include "searchthread.h"
#include <QVariant>

void SearchThread::set_configure(QString file_name, QString subject) {
    this->file_name = file_name;
    this->subject = subject
            .replace("'", "{|}")
            .replace("\"", "{||}");;
}

void SearchThread::run() {
    QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE", "index");
    db.setDatabaseName(this->file_name);
    db.open();
    QList<Searched> searched_list;
    QSqlQuery q = db.exec(QString("SELECT `line_text`, `filename`, `line_number` FROM `index` WHERE ") +
                             QString("`word` LIKE '%") + this->subject + QString("%' ") +
                             QString("ORDER BY `id` ASC"));
    while (q.next()) {
        Searched item;
        item.code = q.value("line_text").toString()
                .replace("{|}", "'")
                .replace("{||}", "\"");
        item.file_name = q.value("filename").toString();
        item.line_number = q.value("line_number").toInt();
        searched_list.append(item);
    }
    QVariant v;
    v.setValue(searched_list);
    emit this->send_list(v);
    db.close();
    QSqlDatabase::removeDatabase("index");
}
